package com.java2207.group1.controller;

import com.java2207.group1.common.LayuiPageVo;
import com.java2207.group1.common.R;
import com.java2207.group1.pojo.Dorm;
import com.java2207.group1.service.DormService;
import com.java2207.group1.util.OssUtil;
import com.java2207.group1.vo.Breakfast.BarVo;
import com.java2207.group1.vo.Breakfast.PieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author :xs
 * @date : 2022/7/26
 */

@RestController
@RequestMapping("/dorm")
public class DormController {

    @Autowired
    private DormService dormService;

    //分页
    @RequestMapping("/listDorm")
    public LayuiPageVo<Dorm> dormPageData(@RequestParam Map<String, Object> params) {
        return dormService.dormPage(params);
    }


    //删除
    @RequestMapping("/deleteDorm")
    public R deleteDorm(Integer id) {
        int i = dormService.deleteDorm(id);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //多选删除
    @GetMapping("/deleteDormsById")
    public R deleteDormsById(String ids) {
        int result = dormService.deleteDormsById(ids);
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/addDorm")
    public R addDorm(Dorm dorm) {
        int i = dormService.addDorm(dorm);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/updateDorm")
    public R updateDorm(Dorm dorm) {
        int i = dormService.updateDorm(dorm);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/upload")
    public R uploadPic(MultipartFile file) throws IOException {

        if (file != null) {
            //得到文件名
            String originalFilename = file.getOriginalFilename();

            InputStream inputStream = file.getInputStream();

            String url = OssUtil.uploadFile((FileInputStream) inputStream, originalFilename);

            //把文件 url 写给前端

            return R.ok().data("url", url);
        }
        return R.ok();
    }


    @RequestMapping("/echaetsPie")
    public R echaetsPie(){
        List<PieVo>list = dormService.TypeNameData();

        return  R.ok().data("typePie",list);
    }

    @RequestMapping("/echartBar")
    public  R echartBar(){
        List<BarVo> list = dormService.TypeNameBarData();
        return R.ok().data("typeBar",list);
    }

}